//
//  ShopListsViewController.m
//  Jewelry
//
//  Created by HJ on 2017/1/6.
//  Copyright © 2017年 HJ. All rights reserved.
//

#import "ShopListsViewController.h"
#import "ShopsCollectionViewCell.h"
#import "ShopDetailViewController.h"
#import "RankView.h"
#import "ScreenView.h"
#import "SreenRankView.h"
#import "ClassModle.h"
#import "SortTagsView.h"
#import "MCRecipeSearchHistoryManager.h"
@interface ShopListsViewController ()<RankViewDelegate, UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate,UITextFieldDelegate,ScreenViewDelegate,ScreenRankDelegate,SortViewClickDelegate>

@property (nonatomic, strong) ScreenView * screenView;
@property (nonatomic, strong) UIView     * alpheView;
@property (nonatomic, strong) RankView   * rankView;
@property (nonatomic, strong) SBView     * sbView;
@property (nonatomic ,strong) UIView     * scView;
@property (nonatomic ,strong) UIView     * btnView;
@property (nonatomic ,strong) UIView     * blankView;
@property (nonatomic, retain) UIButton   * lastBtn;
@property (nonatomic, strong) ClassModle * shopModle;
@property (nonatomic, strong) SortTagsView * sortView;
@property (nonatomic, strong) NSMutableArray    * ScArray;
@property (nonatomic, strong) NSMutableArray    * standardValueList;
@property (nonatomic, strong) NSMutableArray    * standardList;
@property (nonatomic, strong) NSMutableArray    * standardListID;
@property (nonatomic, strong) NSMutableArray    * resultArray;
@property (nonatomic, strong) NSMutableArray    * standardValueID;
@property (strong, nonatomic) UICollectionView      *collectionView;
@property (nonatomic, strong) NSMutableArray        * goodsArray;
@property (nonatomic, strong) NSMutableArray        * menuMainArray;
@property (nonatomic, strong) NSMutableDictionary   * lastDic;
@property (nonatomic, assign) int  pageNo;
// 0查询所有，传入1查询有货
@property (nonatomic, assign) int  def;
// 1最新2最旧3访问 最多4访问 最小5价格 最多6价格 最小
@property (nonatomic, assign) int  status;
@property (nonatomic, assign) BOOL Isopen;
//测试数据
@property (assign, nonatomic) CGFloat offetHeight;
@end

@implementation ShopListsViewController


-(NSMutableArray*)menuMainArray{
    if (!_menuMainArray) {
        _menuMainArray=[NSMutableArray array];
    }
    return _menuMainArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //商品列表
    //[self loadShopList];
    //筛选列表
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageNo = 1;
    if (self.origin==3) {
        [self sortClick:self.dic];
	}else if(self.origin == 4){
		[self creatEveryThingUi:self.requestDic];
	}
	else{
        [self loadSearchData];
    }
    self.view.backgroundColor = [UIColor whiteColor] ;
    self.ScArray=[[NSMutableArray alloc]init];;
    [self.ScArray addObjectsFromArray:[[MCRecipeSearchHistoryManager sharedInstance]allObject]];
    [self createdUI];
     self.Isopen=NO;
    self.standardList = [[NSMutableArray alloc]init];;
    self.standardListID = [[NSMutableArray alloc]init];;
    self.standardValueList = [[NSMutableArray alloc]init];
    self.standardValueID = [[NSMutableArray alloc]init];
     self.lastDic = [[NSMutableDictionary alloc]init];
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(notice:) name:@"123" object:nil];
    self.blankView=[ZJUITools showBlankView];
    self.blankView.frame=CGRectMake(0, 45, MainW, MainH);
    self.blankView.hidden=YES;
    [self.view addSubview:self.blankView];


    
}
-(NSMutableArray *)rankArray{
    
    if (_rankArray == nil) {
        
        _rankArray = [[NSMutableArray alloc] init];
    }
    return _rankArray;
}
-(NSMutableDictionary *)rankDic{
    
    if (_rankDic == nil) {
        
        _rankDic = [[NSMutableDictionary alloc] init];
    }
    return _rankDic;
}
#pragma mark --------- 右边图片
-(void)notice:(id)sender{
    NSLog(@"%@",sender);
}
- (void)rightClick
{
    NSLog(@"弹出筛选");
}

-(SortTagsView *)sortView
{
	if (_sortView == nil) {
		_sortView = [[SortTagsView alloc] initWithFrame:CGRectMake(0, 0, MainW, 40)];
		_sortView.delegate=self;
		_sortView.backgroundColor=[UIColor whiteColor];
	}
	return _sortView;
}

- (void)createdUI
{
//    self.sortView = [[SortTagsView alloc] initWithFrame:CGRectMake(0, 0, MainW, 40)];
//    self.sortView.delegate=self;
//    self.sortView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.sortView];
    self.sortView.totleBtn.selected=YES;
    //156  28
    self.sbView = [[SBView alloc] initWithFrame:CGRectMake(0, 0, MainW-120, 28)];
    self.sbView.textfied.delegate=self;
    self.navigationItem.titleView =  self.sbView;
    //184
    [self.view addSubview:self.collectionView];
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 44)];
    [btn setTitle:MyString(@"搜索") forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:12];
    [btn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item=[[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setRightBarButtonItem:item];
//    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithTitle:MyString(@"搜索") style:UIBarButtonItemStyleDone target:self action:@selector(searchBtnClick)];
//    [item setTitleTextAttributes:@{NSFontAttributeName:FontAndStyle14,NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    //self.navigationItem.rightBarButtonItem=item;
    self.scView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64)];
    self.scView.backgroundColor=Gray_Color;
    [self.view addSubview:self.scView];
    if (self.origin==1) {
        self.scView.hidden=YES;
    }else if(self.origin==2){
        self.scView.hidden=NO;
    }else if (self.origin==3){
        self.scView.hidden=YES;
	}else if(self.origin == 4){
		self.scView.hidden = YES;
	}
    self.btnView=[[UIView alloc]initWithFrame:CGRectMake(0, 40, MainW, MainH-64-40)];
    self.btnView.backgroundColor=[UIColor clearColor];
    [self.scView addSubview:self.btnView];
    [self creatSearchView];
    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    return YES;
}
//搜索
-(void)searchBtnClick{
    NSLog(@"搜索");
    if (self.sbView.textfied.text.length==0||[self isEmpty:self.sbView.textfied.text]==YES) {
        [MBProgressHUD showError:@"请输入要搜索的内容"];
        return;
    }else{
         self.lastDic[@"keyword"]=self.sbView.textfied.text;
    }
    [[MCRecipeSearchHistoryManager sharedInstance]addObject:self.sbView.textfied.text];
    //[_history addObject:self.sbView.textfied.text];
    self.pageNo=1;
    [self.menuMainArray removeAllObjects];
    [self sortClick:self.lastDic];
    self.scView.hidden=YES;
    [self.sbView endEditing:YES];
}
-(void)showScreenView{
    self.alpheView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64)];
    self.alpheView.backgroundColor=[UIColor blackColor];
    self.alpheView.hidden=YES;
    self.alpheView.alpha=0.2;
    [self.view addSubview:self.alpheView];
    self.screenView=[[ScreenView alloc]initWithFrame:CGRectMake(MainW, 0, MainW, MainH-64)];
    //self.screenView.hidden=YES;
    self.screenView.delegate=self;
    [self.view addSubview:self.screenView];
    CGFloat maxY = 80;
    CGFloat height = 0;
    for (int i = 0; i < self.standardList.count; i ++)
    {
        
        self.chooseRank = [[SreenRankView alloc] initWithTitle:self.standardList[i] titleArr:self.standardValueList[i] idArr:self.standardValueID[i]  andFrame:CGRectMake(0, maxY, MainW, 40)];
        maxY = CGRectGetMaxY(self.chooseRank.frame);
        height += self.chooseRank.bsg_height;
        self.chooseRank.tag = 800+i;
        self.chooseRank.delegate = self;
        //self.chooseRank.backgroundColor=[UIColor redColor];
        [self.screenView.mainscrollview addSubview:self.chooseRank];
    }
    self.offetHeight=height;
    self.screenView.mainscrollview.contentSize = CGSizeMake(0, height+100);

}
-(void)showRank:(NSArray *)array{
    self.rankView=[[RankView alloc]initWithFrame:CGRectMake(0, 41, MainW, MainH-64-41) ];
    //NSArray*arry=[NSArray arrayWithObjects:@"1",@"1",@"1",@"1", nil];
    [self.rankView creatWithArray:array];
    self.rankView.hidden=YES;
    self.rankView.delegate=self;
    [self.view addSubview:self.rankView];
}
#pragma mark ------ rankView的代理方法
-(void)ChooseButtonClicked:(NSMutableDictionary *)sender{
    self.rankView.hidden=YES;
     self.Isopen=NO;
    self.sortView.totalImg.image=[UIImage imageNamed:@"composite_arrow"];
     self.sortView.totleBtn.selected=YES;
    self.lastDic[@"sortMethod"]=[sender valueForKey:@"sortMethod"];
    self.lastDic[@"sortType"]=[sender valueForKey:@"sortType"];
    self.pageNo=1;
    [self.menuMainArray removeAllObjects];
    [self sortClick:self.lastDic];
    NSLog(@"%ld",(long)sender);
}
-(void)CancelButtonClicked{
    self.sortView.totalImg.image=[UIImage imageNamed:@"composite_arrow"];
    self.sortView.totleBtn.selected=YES;
    self.rankView.hidden=YES;
    self.Isopen=NO;
}
-(void)firstTotalSortClick{
    if (self.lastDic!=nil) {
        [self sortClick:self.lastDic];
    }else{
        if (self.Isopen==YES) {
            self.rankView.hidden=YES;
            self.sortView.totalImg.image = [UIImage imageNamed:@"composite_arrow_up"];
            self.Isopen=NO;
        }else{
            self.rankView.hidden=NO;
            self.Isopen=YES;
            self.sortView.totalImg.image = [UIImage imageNamed:@"composite_arrow"];
        }

    }
    
}
-(void)totalSortClick{
    if (self.Isopen==YES) {
        self.rankView.hidden=YES;
        self.Isopen=NO;
        self.sortView.totalImg.image = [UIImage imageNamed:@"composite_arrow"];
    }else{
        self.rankView.hidden=NO;
        self.Isopen=YES;
        self.sortView.totalImg.image = [UIImage imageNamed:@"composite_arrow_up"];
    }
    NSLog(@"综合");
}
-(void)varSortClick:(NSMutableDictionary *)dic{
    self.pageNo=1;
    [self.menuMainArray removeAllObjects];
    if (self.Isopen==YES) {
        self.rankView.hidden=YES;
        self.Isopen=NO;
    }
    self.lastDic[@"sortMethod"]=[dic valueForKey:@"sortMethod"];
    self.lastDic[@"sortType"]=[dic valueForKey:@"sortType"];
    [self sortClick:self.lastDic];

}
-(void)screenSortClick{
     self.alpheView.hidden=NO;
    [UIView animateWithDuration: 0.35 animations: ^{
        self.screenView.frame =CGRectMake(0, 0, MainW, MainH-64);
    } completion: nil];
    if (self.Isopen==YES) {
        self.rankView.hidden=YES;
        self.Isopen=NO;
        self.sortView.totalImg.image=[UIImage imageNamed:@"composite_arrow"];
        self.sortView.totleBtn.selected=YES;
    }
    NSLog(@"筛选");
}
-(void)disMiss{
    [self.screenView endEditing:YES];
    self.alpheView.hidden=YES;
    [UIView animateWithDuration: 0.35 animations: ^{
        //        self.backgroundView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
        self.screenView.frame =CGRectMake(MainW, 0, MainW, MainH-64);
    } completion: nil];
}
#pragma mark ----- 商品列表
- (void)loadShopListData
{
}

#pragma mark --- Getter
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        //设置滚动方向
        [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //左右间距
        flowlayout.minimumInteritemSpacing =5;
        //上下间距
        flowlayout.minimumLineSpacing =5 ;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, MainW, MainH-40-65) collectionViewLayout:flowlayout];
        _collectionView.backgroundColor = Gray_Color;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"ShopsCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ShopsCell"];
        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
        
    }
    return _collectionView;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.menuMainArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.shopModle = self.menuMainArray[indexPath.row];
    ShopsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShopsCell" forIndexPath:indexPath];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:self.shopModle.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    cell.titleLabel.text=self.shopModle.title;
    cell.moneyLabel.text=[NSString stringWithFormat:@"¥ %@",self.shopModle.fixedPrice];
    cell.stateLabel.text=[NSString stringWithFormat:@"%@:%@件",MyString(@"月销量"),self.shopModle.volume];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.shopModle = self.menuMainArray[indexPath.row];
    ShopDetailViewController * VC = [[ShopDetailViewController alloc] init];
    VC.spuId=self.shopModle.skuId;
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.shopModle = self.menuMainArray[indexPath.row];
    CGFloat height =[ZJUITools getHeightByWidth:self.view.frame.size.width/2-10 title:self.shopModle.title font:FontAndStyle14] + (MainW-15)/2+20;
    return CGSizeMake((MainW-15)/2, height);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);//分别为上、左、下、右
}
#pragma mark -------------- 商品列表api
- (void)loadShopList
{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init
                              ];
    dic[@"cateId"]=self.ids;
    dic[@"page"]=@(1);
    dic[@"pageRows"]=@(10);
   [ self doHttpRequestWithApi:API_filterAndSort_LIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
       self.menuMainArray=[ClassModle mj_objectArrayWithKeyValuesArray:dic[@"data"]];
       [self.collectionView reloadData];
       //NSLog(@"有什么辅助 %@",dic);
   }];
}
#pragma mark -------------- 筛选列表api
-(void)loadSearchData{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    self.lastDic[@"cateId"]=self.ids;
    dic[@"cateId"]=self.ids;
    dic[@"pageSize"]=@(10);
    dic[@"pageNumber"]=@(self.pageNo);
    [self doHttpRequestWithApi:API_SearchCate method:POST dic:dic successBlock:^(NSDictionary *dic) {
        NSLog(@"大大大 %@",dic[@"data"]);
        [self creatEveryThingUi:dic];
        
    }];
}
-(void)creatEveryThingUi:(NSDictionary *)dic{
    NSArray *sortAray=dic[@"data"][@"sortFields"][@"common"];
    NSArray *rankAray=dic[@"data"][@"sortFields"][@"uncommon"];
    [self createSortView:sortAray];
    [self showRank:rankAray];
    //侧面筛选
    NSArray *arry=dic[@"data"][@"attrOptions"];
    for (NSDictionary*dic in arry) {
        NSString *string=dic[@"name"];
        NSString *idStr=dic[@"attrId"];
        [self.standardList addObject:string];
        [self.standardListID addObject:idStr];
    }
    for (NSDictionary*dic in arry ) {
        NSArray *array=dic[@"options"];
        NSMutableArray *vlauAry=[[NSMutableArray alloc]init];
        NSMutableArray *idAry=[[NSMutableArray alloc]init];
        //[vlauAry removeAllObjects];
        for (NSDictionary *dic in array) {
            NSString*string=dic[@"name"];
            NSString*idstr=dic[@"optionId"];
            [vlauAry addObject:string];
            [idAry addObject:idstr];
        }
        [self.standardValueList addObject:vlauAry];
        [self.standardValueID addObject:idAry];
        
    }
    NSLog(@"标题:   内容:   id:%@",self.standardValueID);
    [self showScreenView];
    [self.menuMainArray addObjectsFromArray:[ClassModle mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]]];
    if (self.menuMainArray.count==0) {
        self.blankView.hidden=NO;
    }else{
         self.blankView.hidden=YES;
        [self.collectionView reloadData];
    }
    
    
}
#pragma mark ----------- 尾部刷新
-(void)CancelBtnClcik{
    [self disMiss];
    if (self.screenView.minPrince.text.length==0 &&self.screenView.maxPrince.text.length==0&&self.resultArray.count==0) {
        self.sortView.screenBtn.selected=NO;
        self.sortView.screenImag.image=[UIImage imageNamed:@"screen_nor"];
        self.sortView.screenlabel.textColor=TextColor3;
    }

    NSLog(@"筛选的取消按钮");
}
-(void)againBtnClick{
    for (int i=0; i < _standardList.count; i++)
    {
        SreenRankView *view = [self.view viewWithTag:800+i];
        
        for (UIButton *obj in  view.btnView.subviews)
        {
            if (obj.selected) {
                obj.selected=NO;
                [obj setTitleColor:TextColor3 forState:UIControlStateNormal];
                obj.backgroundColor = SeparatorColor;
                obj.layer.borderWidth=0.5;
                obj.layer.borderColor=[UIColor whiteColor].CGColor;
            }
        }
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"isReset" object:nil];
    [self.rankArray removeAllObjects];
    [self.resultArray removeAllObjects];
}
-(void)sureBtnClick{
    //self.ids=@"";
    //NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    if (self.screenView.minPrince.text.length==0 &&self.screenView.maxPrince.text.length==0&&self.resultArray.count==0) {
        self.sortView.screenBtn.selected=NO;
        self.sortView.screenImag.image=[UIImage imageNamed:@"screen_nor"];
        self.sortView.screenlabel.textColor=TextColor3;
    }
    self.lastDic[@"options"]=[self.resultArray mj_JSONString];
    self.lastDic[@"bottomPrice"]=self.screenView.minPrince.text;
    self.lastDic[@"topPrice"]=self.screenView.maxPrince.text;
    self.pageNo=1;
    [self.menuMainArray removeAllObjects];
    [self sortClick:self.lastDic];
    [self disMiss];
    
}
-(void)selectBtnTitle:(NSString *)title andBtn:(UIButton *)btn andArray:(NSMutableArray *)array{
    if ([self.screenView.minPrince.text intValue]>[self.screenView.maxPrince.text intValue]) {
        NSString* a=self.screenView.maxPrince.text;
        self.screenView.maxPrince.text=self.screenView.minPrince.text;
        self.screenView.minPrince.text=a;
    }

    [self.screenView endEditing:YES];
    NSLog(@"传过来的数组 :%@",array);
   // [self.rankArray removeAllObjects];
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    for (int i=0; i < _standardList.count; i++)
    {
        
        SreenRankView *view = [self.view viewWithTag:800+i];
        
        for (UIButton *obj in  view.btnView.subviews)
        {
            if([obj isEqual:btn]){
                
                [dic setObject:array forKey:@"oid"];
                [dic setObject:self.standardListID[i] forKey:@"aid"];
                [self.rankArray addObject:dic];
            }else{
                if (array.count==0) {
                    [self.rankArray removeObject:dic];
                }else{
                    if (![self.rankArray containsObject:dic]) {
                        [self.rankArray addObject:dic];
                    }

                }
                
            }
        }
        
    }
    NSSet *set=[NSSet setWithArray:self.rankArray];
    self.resultArray=[NSMutableArray arrayWithArray:[set allObjects]];
    for (int i = 0; i < self.resultArray.count; i++) {
        NSDictionary *dic=self.resultArray[i];
        NSArray *arry=dic[@"oid"];
        if (arry.count==0) {
            [self.resultArray removeObject:dic];
        }
    }
    NSLog(@"%@",self.resultArray);

    
}
-(void)createSortView:(NSArray *)array{
       [self.sortView creatSortTagsView:array];
    
}
-(void)loadsearchRecomData{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"keyword"]=self.sbView.textfied.text;
    [self doHttpRequestWithApi:API_SEARCHRECOM method:GET dic:dic successBlock:^(NSDictionary *dic) {
        
    }];
}
-(void)seachshopList:(NSMutableDictionary *)dic{
    dic[@"pageSize"]=@(10);
    dic[@"pageNumber"]=@(self.pageNo);
    //dic[@"cateId"]=self.ids;
    [self doHttpRequestWithApi:API_Search method:POST dic:dic successBlock:^(NSDictionary *dic) {
        if (self.origin==3) {
            [self creatEveryThingUi:dic];
        }
        [self.menuMainArray addObjectsFromArray:[ClassModle mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]]];
        if (self.menuMainArray.count==0) {
            self.blankView.hidden=NO;
        }else{
            self.blankView.hidden=YES;
        }
        [self.collectionView reloadData];
        
    }];
    NSLog(@"dic:  %@",dic);
}
-(void)sortClick:(NSMutableDictionary *)dic{
    dic[@"pageSize"]=@(10);
    dic[@"pageNumber"]=@(self.pageNo);
    dic[@"cateId"]=self.ids;
    [self doHttpRequestWithApi:API_SearchCate method:POST dic:dic successBlock:^(NSDictionary *dic) {
        if (self.origin==3) {
            [self creatEveryThingUi:dic];
            return ;
        }
        [self.menuMainArray addObjectsFromArray:[ClassModle mj_objectArrayWithKeyValuesArray:dic[@"data"][@"list"]]];
        if (self.menuMainArray.count==0) {
            self.blankView.hidden=NO;
        }else{
            self.blankView.hidden=YES;
            
        }
        [self.collectionView reloadData];

    }];
    NSLog(@"dic:  %@",dic);
    
}
-(void)creatSearchView{
    
    MYLabel *label=[[MYLabel alloc]initWithFrame:CGRectMake(15, 15, MainW-30, 15)];
    label.text=MyString(@"最近搜索");
    label.font=[UIFont systemFontOfSize:15];
    label.textColor=TextColor3;
    [self.scView addSubview:label];
    UIImageView*iconDele=[[UIImageView alloc]initWithFrame:CGRectMake(MainW-20, label.bsg_y-1, 10, 12)];
    iconDele.image=[UIImage imageNamed:@"icon_delete"];
    [self.scView addSubview:iconDele];
    UIButton *deletebtn=[[UIButton alloc]initWithFrame:CGRectMake(MainW-40,0 , 40, 40)];
    [deletebtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scView addSubview:deletebtn];
    int count = 0;
    float btnWidth = 0;
    float viewHeight = 0;
    NSArray *Array=[[NSArray alloc]init];
    Array=[[MCRecipeSearchHistoryManager sharedInstance] allObject];
    NSLog(@"  搜索历史:%@",Array);
    for (int i = 0; i < Array.count; i++) {
        
        NSString *btnName = Array[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        btn.titleLabel.font = FontAndStyle12;
        [btn setTitle:btnName forState:UIControlStateNormal];
        //btn.titleLabel.lineBreakMode =  NSLineBreakByWordWrapping;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        
        //NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(12) forKey:NSFontAttributeName];
        CGSize btnSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font }];
        
        btn.width = btnSize.width + 15;
        btn.height = btnSize.height + 12;
        
        if (i==0)
        {
            btn.x = 25;
            btnWidth += CGRectGetMaxX(btn.frame);
        }
        else{
            btnWidth += CGRectGetMaxX(btn.frame)+20;
            if (btnWidth > MainW) {
                count++;
                btn.x = 25;
                btnWidth = CGRectGetMaxX(btn.frame);
            }
            else{
                
                btn.x += btnWidth - btn.bsg_width;
            }
        }
        btn.y += count * (btn.bsg_height+10)+10;
        
        viewHeight = CGRectGetMaxY(btn.frame)+10;
        
        [self.btnView addSubview:btn];
        
        btn.tag = 100+i;
        
        
        //        if ([btnName isEqualToString:self.selectStr])
        //        {
        //            self.selectBtn = btn;
        //            self.selectBtn.selected = YES;
        //            self.selectBtn.backgroundColor = [UIColor greenColor];
        //        }
        
    }
    
}
-(void)deleteBtnClick{
    LXAlertView *alertView=[[LXAlertView alloc]initWithTitle:@"" message:MyString(@"确定删除历史搜索记录?") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
        if (clickIndex==1) {
            [[MCRecipeSearchHistoryManager sharedInstance]removeAllObject];
            for (UIView *view in self.btnView.subviews) {
                [view removeFromSuperview];
            }
            [self creatSearchView];
        }
    }];
    [alertView showLXAlertView];
}
-(void)btnClick:(UIButton *)sender{
    self.sbView.textfied.text=self.ScArray[sender.tag-100];
   
        if (self.sbView.textfied.text.length==0) {
            self.lastDic[@"keyword"]=@"";
        }else{
            self.lastDic[@"keyword"]=self.sbView.textfied.text;
        }
    
        [[MCRecipeSearchHistoryManager sharedInstance]addObject:self.sbView.textfied.text];
        //[_history addObject:self.sbView.textfied.text];
    self.pageNo=1;
    [self.menuMainArray removeAllObjects];
    [self sortClick:self.lastDic];
    self.scView.hidden=YES;
    [self.sbView endEditing:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.sbView endEditing:YES];
}
-(void)loadMoreTopic{
    self.pageNo++;
    [self sortClick:self.lastDic];
    [self.collectionView.mj_footer endRefreshing];
}
@end
